package com.dsteiner.xelavos.client.controller;

import com.dsteiner.widgets.client.StringUtil;
import com.dsteiner.xelavos.client.controller.CreateObjectController.ImageSuggestions;
import com.google.gwt.search.client.ImageResult;
import com.google.gwt.search.client.ResultSetSize;
import com.google.gwt.search.client.SearchCompleteHandler;
import com.google.gwt.search.client.SearchControl;
import com.google.gwt.search.client.SearchControlOptions;

public class ImageSearchImpl implements ImageSearch{

	private SearchControl control;
	private String imageSearchText;
	
	public ImageSearchImpl() {
		SearchControlOptions options = new SearchControlOptions();
		com.google.gwt.search.client.ImageSearch imageSearch = new com.google.gwt.search.client.ImageSearch();
	    // Choose no HTML generation for quicker results.
	    imageSearch.setNoHtmlGeneration();
	    imageSearch.setResultSetSize(ResultSetSize.LARGE);
	    options.add(imageSearch);
		control = new SearchControl(options);
	}
	
	public void searchImages(String imageSearchText) {
	    this.imageSearchText = imageSearchText;
		System.out.println("ImageSearchImpl.searchImages() :"+imageSearchText);
	    control.execute(imageSearchText);
	}

	public void addImageSearchCompleteHandler(final ImageSearchCompleteHandler imageSearchCompleteHandler) {
		control.addSearchCompleteHandler(new SearchCompleteHandler(){
			public void onSearchComplete(SearchCompleteEvent event) {
				ImageResult result = (ImageResult) event.getResult();
				com.google.gwt.search.client.ImageSearch search = (com.google.gwt.search.client.ImageSearch) event.getSearch();
				int currentPageIndex = search.getCursor().getCurrentPageIndex();
				imageSearchCompleteHandler.onImageSearchComplete(imageSearchText, result.getThumbnailUrl(), currentPageIndex);
			}
		});
	}

	
}
